package com.example.calendar.data.model

import android.os.Parcelable
import androidx.room.Entity
import androidx.room.PrimaryKey
import kotlinx.parcelize.Parcelize
import org.threeten.bp.LocalDateTime
import org.threeten.bp.LocalTime
import java.util.UUID

/**
 * Represents a calendar event with all its properties
 */
@Parcelize
@Entity(tableName = "events")
data class CalendarEvent(
    @PrimaryKey
    val id: String = UUID.randomUUID().toString(),
    val title: String,
    val description: String? = null,
    val location: String? = null,
    val startDateTime: LocalDateTime,
    val endDateTime: LocalDateTime,
    val isAllDay: Boolean = false,
    val recurrenceRule: String? = null,
    val reminderMinutes: List<Int> = emptyList(),
    val categoryId: String? = null,
    val color: Int? = null,
    val createdAt: LocalDateTime = LocalDateTime.now(),
    val updatedAt: LocalDateTime = LocalDateTime.now(),
    val isDeleted: Boolean = false
) : Parcelable {
    
    /**
     * Check if this event overlaps with the given time range
     */
    fun overlapsWith(startDate: LocalDateTime, endDate: LocalDateTime): Boolean {
        return (startDateTime.isBefore(endDate) || startDateTime.isEqual(endDate)) &&
                (endDateTime.isAfter(startDate) || endDateTime.isEqual(startDate))
    }
    
    /**
     * Check if this event is happening on the given date
     */
    fun isOnDate(date: LocalDateTime): Boolean {
        val startDate = startDateTime.toLocalDate()
        val endDate = endDateTime.toLocalDate()
        val checkDate = date.toLocalDate()
        
        return (checkDate.isEqual(startDate) || checkDate.isAfter(startDate)) &&
                (checkDate.isEqual(endDate) || checkDate.isBefore(endDate))
    }
    
    /**
     * Get the duration of the event in minutes
     */
    fun getDurationMinutes(): Int {
        return if (isAllDay) {
            24 * 60
        } else {
            val startMinutes = startDateTime.hour * 60 + startDateTime.minute
            val endMinutes = endDateTime.hour * 60 + endDateTime.minute
            endMinutes - startMinutes
        }
    }
    
    /**
     * Get the display time string for the event
     */
    fun getTimeRangeString(): String {
        return if (isAllDay) {
            "All day"
        } else {
            val startTime = LocalTime.of(startDateTime.hour, startDateTime.minute)
            val endTime = LocalTime.of(endDateTime.hour, endDateTime.minute)
            "$startTime - $endTime"
        }
    }
} 